#include "htmlformathelper.h"
#include "../Biz/WWCommonUtility.h"
#include <QDateTime>

using namespace wwcommon;

HtmlFormatHelper::HtmlFormatHelper()
{
}
QString HtmlFormatHelper::FormatUserId(const std::string &uid)
{
    QString quid = QString::fromStdString("userid" + uid);
    QString qnick(uid.c_str());
    return "<a href=\"#" + quid + "\">" + qnick + "</a>";
}

QString HtmlFormatHelper::FormatTribeMemberId(const QString& dispName)
{
    int idpos = dispName.lastIndexOf("-");
    QString anchor = "userid" + dispName.mid(idpos + 1);
    return "<a href=\"#" + anchor + "\">" + dispName + "</a>";
}

QString HtmlFormatHelper::FormatTribeId(const std::string& tribeName, const std::string& tribeid)
{
    QString anchor = QString::fromStdString("tribeid" + tribeName + "-" + tribeid);
    return "<a href=\"#" + anchor + "\">" + QString::fromStdString(tribeName + "-" + tribeid)  + "</a>";
}

QString HtmlFormatHelper::FormatMessageContent(const std::string &msg)
{
    return "&nbsp;&nbsp;" + QString(msg.c_str()); //+ "</BLOCKQUOTE>";
}
QString HtmlFormatHelper::FormatTime(time_t time)
{
    QDateTime t;
    t.setTime_t(time);
    t.toLocalTime();
    return t.toString("(MM-dd hh:mm:ss)");
}
QString HtmlFormatHelper::FormatSysMsg(const std::string &msg)
{
    //QString tmp("border:1px solid #EDDFD2;padding-top:2px;padding-right: 4px;padding-bottom:4px;margin-left:6px;margin-right:6px;margin-bottom:5px;margin-top:5px;");
    return QString("<div style=\"color:#000000;background-color:#FEFCE7;\">") +
            QString("<span>系统消息:</span><br/><span>&nbsp;&nbsp;") +
            QString(msg.c_str()) + "</span></div>";
}
QString HtmlFormatHelper::FormatAddedFriendOperation(const std::string &uid, int op)
{
    QString add_anchor = "&nbsp;<a href=\"#AddHim" + QString::fromStdString(uid) + "\">(AddAsFriend)</a>&nbsp;";
    QString agree_and_add_anchor = "&nbsp;<a href=\"#AgreeAddAndAddHim" + QString::fromStdString(uid) + "\">(AgreeAddAndAddHim)</a>&nbsp;";
    QString agreeanchor = "&nbsp;<a href=\"#AgreeAdd" + QString::fromStdString(uid) + "\">(AgreeAdd)</a>&nbsp;";
    QString denyanchor = "&nbsp;<a href=\"#DenyAdd" + QString::fromStdString(uid) + "\">(DenyAdd)</a>&nbsp;";
    QString result;
    if(op & AddHim)
    {
        if(op & AgreeAdd)
            result += agree_and_add_anchor;
        else
            result += add_anchor;
    }
    if(op & AgreeAdd)
    {
        result += agreeanchor;
    }
    if(op & DenyAdd)
    {
        result += denyanchor;
    }
    return result;
}

QString HtmlFormatHelper::FormatPictureMsg(const std::string &picid)
{
    QString picpath = QString::fromStdString(picid);
    return "<a href=\"file:///" + picpath + "\"><img src=\"" + picpath + "\" /></a>";
}

QString HtmlFormatHelper::FormatAllPicturesToHtml(const std::string& inmsg)
{
    std::string allimgs_html;
    std::string rootpath = FileUtility::MakeWWPicSavePath() + "/";
    size_t imgpos = inmsg.find("\\P");
    while(imgpos != std::string::npos)
    {
        // 判断是否是转义字符, ignore
        if(inmsg[imgpos - 1] != '\\')
        {
            // real picture. name is md5 string.
            size_t endpos = inmsg.find("<", imgpos + 2 + 32);
            if(endpos != std::string::npos)
            {
                // 加上点和扩展名,最多5个字符
                if( (endpos - imgpos) > 2 + 32 + 5)
                {
                    printf("endpos:%u, imgpos:%u, data:%s\n", endpos, imgpos, inmsg.c_str());
                    //assert(endpos - imgpos <= 2 + 32 + 5);
                }
                std::string imgname = inmsg.substr(imgpos + 2, endpos - (imgpos + 2));
                allimgs_html += "<a href=\"file:///" + rootpath + imgname + "\"><img src=\"" + rootpath + imgname + "\" /></a>";
                imgpos = endpos - 1;
            }
        }

        imgpos = inmsg.find("\\P", imgpos + 2);
    }
    return QString::fromStdString(allimgs_html);
}
